package cn.pegasus.teamwork.interfaces.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.pegasus.teamwork.interfaces.entity.BussInterfaceParam;
import cn.pegasus.teamwork.interfaces.mapper.BussInterfaceParamMapper;
import cn.pegasus.teamwork.interfaces.service.BussInterfaceParamService;

/**
 * 
 * @author 接口实现层
 *
 */
@Service
public class BussInterfaceParamServiceImpl extends BaseServiceImpl<BussInterfaceParam, BussInterfaceParamMapper> implements BussInterfaceParamService {
	// 注入当前dao对象
	@Autowired
	private BussInterfaceParamMapper bussInterfaceParamMapper;

	public Map<String, Object> addParam(Map<String, Object> map, Long userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			BussInterfaceParam iparam =new BussInterfaceParam();
			iparam.setInterfaceId(Integer.valueOf(map.get("interfaceId").toString()));
			iparam.setParamDesc(map.get("paramDesc").toString());
			iparam.setParamName(map.get("paramName").toString());
			iparam.setParamProperties(map.get("paramProperties").toString());
			iparam.setParamType(Integer.valueOf(map.get("paramType").toString()));
			iparam.setParamRemark(map.get("paramRemark").toString());
			iparam.setCreateTime(new Date());
			iparam.setCreateUserId(userId);
			bussInterfaceParamMapper.insertByEntity(iparam);
			resultMap.put("status", "success");
		} catch (Exception e) {
			resultMap.put("status", "failed");
			resultMap.put("message", "参数保存失败！");
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
	 * 修改接口参数
	 */
	public Map<String, Object> updateParam(Map<String, Object> map, Long userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			BussInterfaceParam iparam =new BussInterfaceParam();
			iparam.setId(Integer.valueOf(map.get("id").toString()));
			iparam.setInterfaceId(Integer.valueOf(map.get("interfaceId").toString()));
			iparam.setParamDesc(map.get("paramDesc").toString());
			iparam.setParamName(map.get("paramName").toString());
			iparam.setParamProperties(map.get("paramProperties").toString());
			iparam.setParamRemark(map.get("paramRemark").toString());
			iparam.setParamType(Integer.valueOf(map.get("paramType").toString()));
			iparam.setLastUpdateTime(new Date());
			iparam.setLastUpdateUserId(userId);
			bussInterfaceParamMapper.updateByEntity(iparam);
			resultMap.put("status", "success");
		} catch (Exception e) {
			resultMap.put("status", "failed");
			resultMap.put("message", "参数保存失败！");
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
	 * 删除接口参数
	 */
	public Map<String, Object> deleteParam(Map<String, Object> map, Long userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int id = Integer.valueOf(map.get("id").toString());
			BussInterfaceParam iparam = new BussInterfaceParam();
			iparam.setId(id);
			bussInterfaceParamMapper.deleteByEntity(iparam);
			resultMap.put("status", "success");
		} catch (Exception e) {
			resultMap.put("status", "failed");
			resultMap.put("message", "参数删除失败！");
			e.printStackTrace();
		}
		return resultMap;
	}

	public void testName() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 查询接口参数
	 */
	public List<BussInterfaceParam> selectParam(Map<String, Object> map) {
		 List<BussInterfaceParam> list=new ArrayList<BussInterfaceParam>();
		 try{
			 BussInterfaceParam param=new BussInterfaceParam();
			 param.setInterfaceId(Integer.valueOf(map.get("interfaceId").toString()));
			 param.setParamType(Integer.valueOf(map.get("paramType").toString()));
			 list= bussInterfaceParamMapper.selectByObject(param);
		 }catch(Exception e){
			e.printStackTrace();
		 }
		return list;
	}
	/**
	 * 修改参数时候弹窗参数显示
	 */
	public BussInterfaceParam detail(Map<String, Object> map){
		BussInterfaceParam param=new BussInterfaceParam();
		List<BussInterfaceParam> list=new ArrayList<BussInterfaceParam>();
		try{
			param.setId(Integer.valueOf(map.get("id").toString()));
			list=bussInterfaceParamMapper.selectByObject(param);
			param=list.get(0);
		}catch(Exception e){
			e.printStackTrace();
		}
		return param;
		
	}
}
